local skel = fk.CreateSkill {
  name = "emo__gongshou",
  attached_skill_name = "emo__gongshou_other&",
}

Fk:loadTranslationTable{
  ["emo__gongshou"] = "共狩",
  [":emo__gongshou"] = "结束阶段，你可以将牌堆顶至多X（X为存活角色数）牌置于武将牌上，你于回合外可使用或打出其中的牌，其他角色出牌阶段限一次，其可以对你使用其中一张牌，然后你也可使用其中一张牌。当你受到伤害后，你可以将任意张“共狩”牌置于牌堆顶。",

  ["#emo__gongshou-invoke"] = "共狩：你可以将牌堆顶牌置于武将牌上",
  ["#emo__gongshou-put"] = "共狩：你可以将任意张“共狩”牌置于牌堆顶",
  ["#emo__gongshou-use"] = "共狩：你可以使用武将牌上的“共狩”牌",

  ["emo__gongshou_other&"] = "共狩",
  [":emo__gongshou_other&"] = "出牌阶段限一次，你可以对有“共狩”的角色使用一张其的“共狩”牌",
  ["#emo__gongshou_other"] = "你可以选择一张“共狩”牌，对其拥有者使用",
}

local EMO = require "packages.moepack.utility.emo_util"

skel:addEffect("viewas", {
  pattern = ".",
  prompt = "#emo__gongshou-use",
  expand_pile = skel.name,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 then
      local card = Fk:getCardById(to_select)
      if Fk.currentResponsePattern == nil then
        return player:canUse(card) and not player:prohibitUse(card)
      else
        return Exppattern:Parse(Fk.currentResponsePattern):match(card)
      end
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return end
    return Fk:getCardById(cards[1])
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function(self, player, response)
    if Fk.currentResponsePattern and player.phase == Player.NotActive then
      return table.find(player:getPile(skel.name), function (id)
        return Exppattern:Parse(Fk.currentResponsePattern):match(Fk:getCardById(id))
      end)
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local num = EMO.chooseNum (player, 1, #player.room.alive_players, "#emo__gongshou-invoke", true)
    if num then
      event:setCostData(self, num)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:addToPile(skel.name, room:getNCards(event:getCostData(self)), true, skel.name)
  end,
})

skel:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and #player:getPile(skel.name) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 1, max_num = 99, skill_name = skel.name, pattern = ".|.|.|"..skel.name, expand_pile = skel.name,
      prompt = "#emo__gongshou-put",
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askToGuanxing(player, {
      cards = event:getCostData(self), skip = true, skill_name = skel.name, bottom_limit = {0, 0}
    }).top
    room:moveCardTo(table.reverse(cards), Card.DrawPile, nil, fk.ReasonPut, skel.name, nil, false)
  end,
})

local skel2 = fk.CreateSkill {
  name = "emo__gongshou_other&",
}

skel2:addEffect("active", {
  prompt = "#emo__gongshou_other",
  expand_pile = function (self, player)
    local ids = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p ~= player and #p:getPile(skel.name) > 0 then
        table.insertTable(ids, p:getPile(skel.name))
      end
    end
    return ids
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and #p:getPile(skel.name) > 0
    end)
  end,
  times = function (self, player)
    return 1 - player:usedSkillTimes(self.name, Player.HistoryPhase)
  end,
  target_filter = function (self, player, to, selected, selected_cards)
    if #selected == 0 and to ~= player and #selected_cards == 1 and table.contains(to:getPile(skel.name), selected_cards[1]) then
      local card = Fk:getCardById(selected_cards[1])
      -- 有次数限制，无距离限制
      return player:canUseTo(card, to, {bypass_distances = true})
    end
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    if #selected > 0 or Fk:getCardById(to_select).is_passive then return false end
    local owner = Fk:currentRoom():getCardOwner(to_select)
    return owner and owner:getPileNameOfId(to_select) == skel.name
  end,
  card_num = 1,
  target_num = 1,
  on_use = function (self, room, use)
    local player = use.from
    local cid = use.cards[1]
    local to = use.tos[1]
    local card = Fk:getCardById(cid)
    room:useCard{ from = player, tos = {to}, card = card }
    if to.dead or #to:getPile(skel.name) == 0 then return end
    room:askToUseRealCard(to, {
      skill_name = skel.name, pattern = ".|.|.|"..skel.name, skip = false, expand_pile = skel.name,
      extra_data = {expand_pile = skel.name},
      prompt = "#emo__gongshou-use",
    })
  end,
})


return {skel, skel2}
